package com.gin.kotlin.no_gay_alliance.provider.ngaphp

import com.gin.kotlin.no_gay_alliance.provider.ngajs.AbstractNgaResourceProvider.Companion.DIR
import com.gin.nga.main.NgaApi
import com.gin.nga.response.base.SingleData
import com.gin.nga.response.topic.TopicKey
import java.lang.ref.WeakReference
import java.util.concurrent.TimeUnit

class TopicKeyProvider private constructor() :
    AbstractCachedDataProvider<Long, SingleData<LinkedHashMap<Int, TopicKey>>, List<TopicKey>>(
        javaType = TYPE_FACTORY.constructCollectionLikeType(List::class.java, TopicKey::class.java),
        TimeUnit.DAYS.toSeconds(2)
    ) {
    override val dir = "TOPIC_KEY"
    override fun transform(rawData: SingleData<LinkedHashMap<Int, TopicKey>>) = rawData.data?.values?.toList() ?: listOf()
    override fun requestMethod(ngaApi: NgaApi, params: Long?) = ngaApi.postApi.topicKey(params!!)
    override fun obtainRelativePath(userId: Long, params: Long?) = "$DIR/$params.json"

    companion object {
        private var instance: WeakReference<TopicKeyProvider> = WeakReference(TopicKeyProvider())
        fun getInstance(): TopicKeyProvider = instance.get() ?: TopicKeyProvider().also { instance = WeakReference(it) }
    }
}